package com.wing.bill.service;

import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.MaterielReportDao;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MaterielReportServiceImpl implements MaterielReportService {
    @Autowired
    private MaterielReportDao materielReportDao;

    @Override
    public JsonResult list(String param, String code) {
        HashMap hm = new HashMap();
        hm.put("title","物料领新统计");
        hm.put("data",materielReportDao.list(ReportUtil.hundredDays().get(0),ReportUtil.hundredDays().get(1),code,param));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(String start, String end, Integer click, String mainCabCode, String code) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (click==0){//显示格子数据
            HashMap map1 = new HashMap(){{put("title","领新总规格数(个)");put("sign","1");}};
            HashMap map2 = new HashMap(){{put("title","未归还规格数(个)");put("sign","2");}};
            HashMap map3 = new HashMap(){{put("title","暂存规格数(个)");put("sign","3");}};
            HashMap map4 = new HashMap(){{put("title","报修规格数(个)");put("sign","4");}};
            HashMap map5 = new HashMap(){{put("title","报废规格数(个)");put("sign","5");}};
            HashMap map6 = new HashMap(){{put("title","报失规格数(个)");put("sign","6");}};
            map1.putAll(materielReportDao.queryTopOne(startTime,endTime,mainCabCode,code));
            map2.putAll(materielReportDao.queryTopTwo(mainCabCode,code));
            map3.putAll(materielReportDao.queryTopThree(startTime,endTime,mainCabCode,code));
            map4.putAll(materielReportDao.queryTopFour(startTime,endTime,mainCabCode,code));
            map5.putAll(materielReportDao.queryTopFive(startTime,endTime,mainCabCode,code));
            map6.putAll(materielReportDao.queryTopSix(startTime,endTime,mainCabCode,code));
            List list = new ArrayList();
            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
            list.add(map6);
            return JsonResult.success("查询成功", list);
        }
        //每个格子点击事件
        if (click==1){
            return JsonResult.success("查询成功", materielReportDao.queryTopOneClick(startTime,endTime,mainCabCode,code));
        }
        if (click==2){
            return JsonResult.success("查询成功",materielReportDao.queryTopTwoClick(mainCabCode,code));
        }
        if (click==3){
            return JsonResult.success("查询成功",materielReportDao.queryTopThreeClick(startTime,endTime,mainCabCode,code));
        }
        if (click==4){
            return JsonResult.success("查询成功",materielReportDao.queryTopFourClick(startTime,endTime,mainCabCode,code));
        }
        if (click==5){
            return JsonResult.success("查询成功",materielReportDao.queryTopFiveClick(startTime,endTime,mainCabCode,code));
        }
        if (click==6){
            return JsonResult.success("查询成功",materielReportDao.queryTopSixClick(startTime,endTime,mainCabCode,code));
        }
        return null;
    }

    @Override
    public JsonResult firstChart(String start, String end, String param, String mainCabCode,String code) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap hm3 = new HashMap();
        hm3.put("title","物料领新占比(按SKU)");
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(materielReportDao.queryFirstChart(startTime,endTime,mainCabCode,code,param));
        map.putAll(materielReportDao.queryFirstChartTotal(startTime,endTime,mainCabCode,code));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }

    @Override
    public JsonResult secondChart(String start, String end, String param, String mainCabCode, String code, String other) {
        LocalDateTime startTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HashMap hm3 = new HashMap();
        if (other.equals("temp")){
            hm3.put("title","物料暂存占比(按SKU)");
        }else if (other.equals("rep")){
            hm3.put("title","物料报修占比(按SKU)");
        }else if (other.equals("rec")){
            hm3.put("title","物料报废占比(按SKU)");
        }else if (other.equals("rep")){
            hm3.put("title","物料报失占比(按SKU)");
        }
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(materielReportDao.querySecondChart(startTime,endTime,mainCabCode,code,param,other));
        map.putAll(materielReportDao.querySecondChartTotal(startTime,endTime,mainCabCode,code,other));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }

    @Override
    public JsonResult thirdChart(String param, String mainCabCode, String code) {
        HashMap hm3 = new HashMap();
        hm3.put("title","物料未归还占比(按SKU)");
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        list1.addAll(materielReportDao.queryThirdChart(mainCabCode,code,param));
        map.putAll(materielReportDao.queryThirdChartTotal(mainCabCode,code));
        map1.put("value",map.get("totalNum"));
        list.add(map);
        hm3.put("totalSum",list);
        hm3.put("data",list1);
        list2.add(map1);
        hm3.put("totalSumOne",list2);
        return JsonResult.success("查询成功",hm3);
    }


    @Override
    public JsonResult listNew(String endTime, String selectParam, String param, String mainCabCode, String enterpriseCode) {
        HashMap hm = new HashMap();
        hm.put("title","刀柜物料统计");

        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(endTime)) {
//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            hm.put("totalValue",materielReportDao.listNewTotal(end,param,mainCabCode,enterpriseCode));
            list.addAll(materielReportDao.listNew(end,selectParam,param,mainCabCode,enterpriseCode));
        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult topCellBySell(String endTime, Integer click, String mainCabCode, String enterpriseCode) {
        if (StringUtils.isNotBlank(endTime)) {//顶部数据  指定时间

//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            if (click == 0) {
                HashMap map1 = materielReportDao.querySellNumAndAmount( end, "qua", mainCabCode,enterpriseCode);
                map1.put("title", "售刀柜库存数量(基础单位)");
                map1.put("sign", "1");

                HashMap map2 = materielReportDao.querySellNumAndAmount( end, "money", mainCabCode,enterpriseCode);
                map2.put("title", "售刀柜在库总金额(元)");
                map2.put("sign", "2");

                HashMap map3 = materielReportDao.querySellAlarm(end, mainCabCode,enterpriseCode);
                map3.put("title", "售刀柜库存告警数(条)");
                map3.put("sign", "3");
                

                List list = new ArrayList();
                list.add(map1);
                list.add(map2);
                list.add(map3);
                return JsonResult.success("查询成功", list);
            } else if (click == 1) {
                return JsonResult.success("查询成功",materielReportDao.querySellNumClick(end,mainCabCode,enterpriseCode));
            } else if (click == 2) {
                return JsonResult.success("查询成功",materielReportDao.querySellAmountClick(end,mainCabCode,enterpriseCode));
            } else if (click == 3) {
                return JsonResult.success("查询成功",materielReportDao.querySellAlarmClick(end,mainCabCode,enterpriseCode));
            }
        }
        return null;
    }

    @Override
    public JsonResult querySellSkuOrCabRanking(String endTime, String selectParam, String param, String mainCabCode, String enterpriseCode, String type,String tab) {
        HashMap hm = new HashMap();
        if ("tab0".equals(tab)) {
            if ("sku".equals(type)) {
                hm.put("title", "物料在库排名(按物料)");
            } else if ("cab".equals(type)) {
                hm.put("title", "物料在库排名(按柜子)");
            }
        } else if ("tab1".equals(tab) && "spu".equals(selectParam)) {
            hm.put("title", "物料在库排名(按系列SPU)");
        } else if ("tab1".equals(tab) && "sku".equals(selectParam)) {
            hm.put("title", "物料在库排名(按SKU)");
        } else if ("tab1".equals(tab) && "type".equals(selectParam)) {
            hm.put("title", "物料在库排名(按类型)");
        } else if ("tab1".equals(tab) && "category".equals(selectParam)) {
            hm.put("title", "物料在库排名(按分类)");
        } else if ("tab1".equals(tab) && "brand".equals(selectParam)) {
            hm.put("title", "物料在库排名(按品牌)");
        } else if ("tab1".equals(tab) && "main".equals(selectParam)) {
            hm.put("title", "物料在库排名(按主柜)");
        } else if ("tab1".equals(tab) && "desk".equals(selectParam)) {
            hm.put("title", "物料在库排名(按副柜)");
        } else if ("tab1".equals(tab) && "space".equals(selectParam)) {
            hm.put("title", "物料在库排名(按货道)");
        }


        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(endTime)) {
//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            if ("space".equals(selectParam)) {
                list.addAll(materielReportDao.querySellSpaceCodeRanking(end,selectParam,param,mainCabCode,enterpriseCode));
            } else {
                list.addAll(materielReportDao.querySellSkuAndCabRanking(end, selectParam, param, mainCabCode, enterpriseCode));
            }
        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult topCellByStore( String endTime, Integer click, String mainCabCode, String enterpriseCode) {
        if ( StringUtils.isNotBlank(endTime)) {//顶部数据  指定时间

//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            if (click == 0) {
                HashMap map1 = materielReportDao.queryStoreNumAndAmount(end, "qua", mainCabCode,enterpriseCode);
                map1.put("title", "存刀柜库存数量(基础单位)");
                map1.put("sign", "1");

                HashMap map2 = materielReportDao.queryStoreNumAndAmount(end, "money", mainCabCode,enterpriseCode);
                map2.put("title", "存刀柜在库总金额(元)");
                map2.put("sign", "2");

                HashMap map3 = materielReportDao.queryStoreAlarm(end, mainCabCode,enterpriseCode);
                map3.put("title", "存刀柜库存告警数(条)");
                map3.put("sign", "3");

                HashMap map4 = materielReportDao.queryStoreTempNum( end, mainCabCode,enterpriseCode);
                map4.put("title", "暂存刀具数量");
                map4.put("title2", "暂存刀具数量(公共)");
                map4.put("title3", "暂存刀具数量(专人)");
                map4.put("sign", "4");

                HashMap map5 = materielReportDao.queryStoreRepairAndRecycle(end, mainCabCode,enterpriseCode,2);
                map5.put("title", "报修数量(待盘验)");
                map5.put("sign", "5");

                HashMap map6 = materielReportDao.queryStoreRepairAndRecycle(end, mainCabCode,enterpriseCode,3);
                map6.put("title", "报废数量(待盘验)");
                map6.put("sign", "6");


                List list = new ArrayList();
                list.add(map1);
                list.add(map2);
                list.add(map3);
                list.add(map4);
                list.add(map5);
                list.add(map6);
                return JsonResult.success("查询成功", list);
            } else if (click == 1) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreNumAndAmountClick(end,"qua",mainCabCode,enterpriseCode));
            } else if (click == 2) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreNumAndAmountClick(end,"money",mainCabCode,enterpriseCode));
            } else if (click == 3) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreAlarmClick(end,mainCabCode,enterpriseCode));
            } else if (click == 4) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreTempNumClick(end,mainCabCode,enterpriseCode));
            } else if (click == 5) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreRepairAndRecycleClick(end,mainCabCode,enterpriseCode,2));
            } else if (click == 6) {
                return JsonResult.success("查询成功",materielReportDao.queryStoreRepairAndRecycleClick(end,mainCabCode,enterpriseCode,3));
            }
        }
        return null;
    }

    @Override
    public JsonResult queryStoreSkuOrCabRanking(String endTime, String selectParam, String param, String mainCabCode, String enterpriseCode, String type, String tab) {
        HashMap hm = new HashMap();
        if ("tab0".equals(tab)) {
            if ("sku".equals(type)) {
                hm.put("title", "物料在库排名(按物料)");
            } else if ("cab".equals(type)) {
                hm.put("title", "物料在库排名(按柜子)");
            }
        } else if ("tab1".equals(tab) && "spu".equals(selectParam)) {
            hm.put("title", "物料在库排名(按系列SPU)");
        } else if ("tab1".equals(tab) && "sku".equals(selectParam)) {
            hm.put("title", "物料在库排名(按SKU)");
        } else if ("tab1".equals(tab) && "type".equals(selectParam)) {
            hm.put("title", "物料在库排名(按类型)");
        } else if ("tab1".equals(tab) && "category".equals(selectParam)) {
            hm.put("title", "物料在库排名(按分类)");
        } else if ("tab1".equals(tab) && "brand".equals(selectParam)) {
            hm.put("title", "物料在库排名(按品牌)");
        } else if ("tab1".equals(tab) && "main".equals(selectParam)) {
            hm.put("title", "物料在库排名(按主柜)");
        } else if ("tab1".equals(tab) && "desk".equals(selectParam)) {
            hm.put("title", "物料在库排名(按副柜)");
        } else if ("tab1".equals(tab) && "space".equals(selectParam)) {
            hm.put("title", "物料在库排名(按货道)");
        }

        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(endTime)) {
//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            if ("space".equals(selectParam)) {
                list.addAll(materielReportDao.queryStoreSpaceCodeRanking(end,selectParam,param,mainCabCode,enterpriseCode));
            } else {
                list.addAll(materielReportDao.queryStoreSkuAndCabRanking(end, selectParam, param, mainCabCode, enterpriseCode));
            }
        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult queryStoreCreatorRanking(String endTime, String param, String mainCabCode, String enterpriseCode) {
        HashMap hm = new HashMap();
        hm.put("title","物料在库排名(按存入人)");

        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(endTime)) {
//            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            list.addAll(materielReportDao.queryStoreCreatorRanking(end,param,mainCabCode,enterpriseCode));
        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }
}
